package cn.school.controller.course;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.course.entity.CourseSessions;
import cn.school.course.service.CourseSessionsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @className: CourseSessionsController
 * @author: fangjin.tao
 * @date: 2023/1/5
 **/
@RestController
@Api(tags = "CourseSessionsController => 课程节次管理")
@RequestMapping("/course/sessions")
public class CourseSessionsController {
	@Autowired
	private CourseSessionsService courseSessionsService;
	
	@GetMapping("list")
	public CommonResult<CommonPage<CourseSessions>> list(@RequestParam Map<String, String> params) {
		String pageSize = params.get("pageSize");
		String pageNum = params.get("pageNum");
		IPage page = new Page(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		LambdaQueryWrapper<CourseSessions> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		String tenantId = SysUserUtils.getSysUser().getTenantId();
		if(!StringUtils.isEmpty(SysUserUtils.getStudentUser().getTenantId())){
			tenantId = SysUserUtils.getStudentUser().getTenantId();
		}
		if(!StringUtils.isEmpty(SysUserUtils.getParentUser().getTenantId())){
			tenantId = SysUserUtils.getParentUser().getTenantId();
		}
		lambdaQueryWrapper.eq(CourseSessions::getTenantId, tenantId);
		page = this.courseSessionsService.page(page, lambdaQueryWrapper);
		return CommonResult.success(CommonPage.restPage(page));
	}
	
	@PostMapping("createOrUpdate")
	public CommonResult createOrUpdate(@RequestBody CourseSessions[] courseSessions) {
		this.courseSessionsService.saveOrUpdateBiz(courseSessions);
		return CommonResult.success();
	}
}
